package com.roy.galaxy.thread;

/**
 * @author gang.wang1903@xiaobao100.cn
 * @Date: 2019-10-09 14:45
 *
 * 线程的执行顺序
 */
public class ThreadExcetionSequence {

    public static void main(String[] args) throws InterruptedException {


        threadWait();


    }

    private static void threadWait() throws InterruptedException {
        Thread thread1 = new Thread(ThreadExcetionSequence::action,"t1");

        Thread thread2 = new Thread(ThreadExcetionSequence::action,"t2");

        Thread thread3 = new Thread(ThreadExcetionSequence::action,"t3");

        threadStartAndWait(thread1);
        threadStartAndWait(thread2);
        threadStartAndWait(thread3);
    }


    private static void threadStartAndWait(Thread thread) throws InterruptedException {

        thread.start();
        while (thread.isAlive()){
            synchronized (thread) {
                thread.wait();
            }
        }

    }


    private static void threadOneByOne() throws InterruptedException {
        Thread thread1 = new Thread(ThreadExcetionSequence::action,"t1");

        Thread thread2 = new Thread(ThreadExcetionSequence::action,"t2");

        Thread thread3 = new Thread(ThreadExcetionSequence::action,"t3");

        thread2.start();
        //控制线程必须执行完成
        thread2.join();

        thread1.start();
        thread1.join();

        thread3.start();
        thread1.join();
    }


    public static void action() {
        System.out.println(Thread.currentThread().getName()+"正在执行");
    }
}
